package com.datatraffic.salesup;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.datatraffic.salesup.model.Form;

public class ListAdapter extends ArrayAdapter<Form>{

    Context context; 
    int layoutResourceId; 
    Form data[] = null;
    
    public ListAdapter(Context context, int layoutResourceId, Form[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ItemHolder holder = null;
        
        if(row == null)
        {//Activity
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new ItemHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtCel=(TextView)row.findViewById(R.id.textViewTelefono);
            
            row.setTag(holder);
        }
        else
        {
            holder = (ItemHolder)row.getTag();
        }
        
        	Form item = data[position];
        	holder.txtTitle.setText(item.getNombre()+" "+item.getApellido()+"\n"+item.getFecha_y_hora_proximo_contacto());
        	if(item.getEstado().equalsIgnoreCase("CREADO"))
        	{
        		holder.imgIcon.setImageResource(R.drawable.iconocreado);
        	}
        	else if(item.getEstado().equalsIgnoreCase("CONTACTO 1"))
        	{
        		holder.imgIcon.setImageResource(R.drawable.iconocontacto1);
        	}
        	else if(item.getEstado().equalsIgnoreCase("CONTACTO 2"))
        	{
        		holder.imgIcon.setImageResource(R.drawable.iconocontacto2);
        	}
        	else if(item.getEstado().equalsIgnoreCase("CONTACTO 3"))
        	{
        		holder.imgIcon.setImageResource(R.drawable.iconocontacto3);
        	}
        	else if(item.getEstado().equalsIgnoreCase("VENTA"))
        	{
        		holder.imgIcon.setImageResource(R.drawable.iconovendido);
        	}
        	else if(item.getEstado().equalsIgnoreCase("DECLINADO"))
        	{
        		holder.imgIcon.setImageResource(R.drawable.iconodeclinado);
        	}

        	final String bufer=item.getNumero_celular_1();
        	SpannableString content = new SpannableString(bufer);
        	content.setSpan(new UnderlineSpan(), 0, bufer.length(), 0);
        	holder.txtCel.setText(content);

        	holder.txtCel.setOnClickListener(new View.OnClickListener() {
        		@Override
        		public void onClick(View v) {
        			Intent callIntent = new Intent(Intent.ACTION_CALL);
        			callIntent.setData(Uri.parse("tel:"+bufer));
        			context.startActivity(callIntent);

        		}
        	});
        


        return row;
    }
    
    public void setData(Form[] data)
    {
    	this.data=data;
    }
    
    
    @Override
    public void clear()
    {
    	data=new Form[]
		        {
	            
	        };
    }
    
    
    static class ItemHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtDate;
        TextView txtCel;
    }
}